<?php

class CommentsController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);        
    }

    /**
     * Отображение секции
     */
    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new CommentsModel();
        $forms = new CommentsForm();
        $settings = $model->getSettings($sid);
        $this->view->settings = $settings;
        $oid = 0;

        if ($settings->identifier <> '')
            $oid = $this->getRequest()->getParam($settings->identifier, 0);

        if (USER_ID == 0 && $settings->anonymous > 0){
            $form = $forms->anonymous($sid,array('identifier' => $settings->identifier , 'value' => $oid));
            $this->view->form = $form;
        } elseif (USER_ID > 0) {
            $form = $forms->comment($sid,array('identifier' => $settings->identifier , 'value' => $oid));
            $this->view->form = $form;
        }

        # Сохранение комментария
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $auth = Zend_Auth::getInstance();
                $purifier = new HTMLPurifier();
                if (USER_ID > 0) {
                    $data = array(
                        'sid' => (int) $settings->section,
                        'oid' => (int) $oid,
                        'comment' => $purifier->purify($formData['comment']),
                        'uid' => (int) $auth->getIdentity()->id,
                        'parent' => (int) $formData['reply']
                    );
                    $id = $model->newComment($data);
                } elseif ($settings->anonymous > 0) {
                    $data = array(
                        'sid' => (int) $settings->section,
                        'oid' => (int) $oid,
                        'comment' => $purifier->purify($formData['comment']),
                        'guest' => $formData['name'],
                        'guest_email' => $formData['guest_email'],
                        'parent' => (int) $formData['reply']
                    );
                    $id = $model->newComment($data);
                }
                $form->reset();
            } else {
                $form->populate($formData);
            }
        }

        # Вывод комментариев
        if ($settings->identifier <> '') {
            $oid = $this->getRequest()->getParam($settings->identifier, 0);
            if ($oid > 0)
                $this->view->comments = $model->getComments($settings->section, $oid);
        } else {
            $this->view->comments = $model->getComments($settings->section);
        }
    }

    public function modifyAction() {
        $forms = new CommentsForm();
        $model = new CommentsModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $form = $forms->settings($sid);
        $this->view->form = $form;
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                array_pop($formData);
                if ($formData['section'] > 0) {
                    $pageModel = new PageModel();
                    $section = $pageModel->getSection($formData['section']);
                    $module = $pageModel->getAddons('page', $section->mid);
                    $settings = array(
                        'identifier' => $module->identifier,
                        'section' => $formData['section'],
                        'anonymous' => $formData['anonymous']
                    );
                    if ($module->title == 'reference') {
                        $modelReferences = new ReferencesModel();
                        $ref = $modelReferences->getSectionRef($formData['section']);
                        $reference = $modelReferences->getRefs($ref->rid);
                        if ($reference['param'] <> '')
                            $settings['identifier'] = $reference['param'];
                    }
                } else {
                    $settings = array(
                        'identifier' => '',
                        'section' => 0,
                        'anonymous' => $formData['anonymous']
                    );
                }
                $model->updateSettings($sid, $settings);
            }
        } else {
            $settings = $model->getSettings($sid);
            $form->populate(get_object_vars($settings));
        }
    }

    public function addAction() {
        $model = new CommentsModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model->newSettings($sid);
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
